/*
*	title: leaf.c
*
*	Code for the leaf level of the distributed system.
*	This peace of code determines the creation of a integer vector with 125 elements, 
*	passing it through the socket to the thirdL server.
*
*	Date: 22/09/2013
*	Autors: Danilo Barbosa Porto , Henrique Alberto Rusa
*/

#include<sys/timeb.h>
#include <time.h>
#include <stdio.h>
#include<errno.h>
#include <stdlib.h>
#include "tcptoken.h"

#define GENERATED_VECTOR 125 		// client generates 125 integers to send over socket
#define RECEIVED_VECTOR 1000		// client receives 1000 integers over socket

// function to print a buffer made of a integer vector
void printBuffer(int *vector);

// function to calculate the time interval of execution
float time_dif(struct timeb end, struct timeb start);

/*
*	Main function.
*	Parameters:	leaf <PortNumberToThirdL> <HostServerThirdL> 
*/
int main (int argc, char *argv[]) {

	// variables to calculate the time interval
	struct timeb start;
	struct timeb end;
	struct timeb startTotalSort,endTotalSort;
	
	// arguments verification
	if ( argc < 2) {
		fprintf(stderr,"ERROR [%s] usage leaf <PortNumberToThirdL> <HostServerThirdL>: %s \n",argv[0],strerror(errno));
		exit(0);
	}
	
	// getting port number from first argument
	int portno = atoi(argv[1]);
	
	// client of connection to receive and send the data through socket
	clientoken client;
	
	// Allocating the client, given the port and host to connect
	newClient( &client, portno, argv[2] );
	
	/*  
	*	outputBuffer - buffer used to send 125 integers through the connection 
	*	inputBuffer	- buffer uses to receive 1000 integers through the connection 
	*/
	int *outputBuffer, *inputBuffer;

	outputBuffer = (int *)malloc( GENERATED_VECTOR * sizeof(int) );
	inputBuffer = (int *)malloc( RECEIVED_VECTOR * sizeof(int));

	// error treatment over memory allocation to buffers
	if (( outputBuffer == NULL )||( inputBuffer == NULL)) {
		fprintf(stderr,"ERROR [%s] while allocating buffer memory: %s \n",argv[0], strerror(errno));
		exit(0);
	}
	
	// planting the random seed 
	srand(time(NULL));
	
	// generating random vector to send over connection
	ftime(&startTotalSort);
	int i;	 
	for ( i = 0; i < 125; i++)  outputBuffer[i] = rand()%10000; 
	
	/*
	*	Starting client.
	*	this function tries to connect to a server specified over parameters.
	*	this call do not block the program.
	*/
	clientStart( &client );
	ftime(&start);
	
	/*
	*	Client will send data through the socket if only it has connected properly
	*	and the server is running correctly.
	*	This call is dangerous because it could send less bytes than expected, but 
	*	our problem is running on a controlled system, getting no errors with data sending.
	*/
	clientWrite( &client, (void *)outputBuffer, sizeof(int) * GENERATED_VECTOR);

	/*
	*	Call to receive data over the socket from the server.
	*	The server will send a vector of 1000 integers size
	*/
	clientRead( &client, (void *)inputBuffer, sizeof(int) * RECEIVED_VECTOR );

	// printing the received buffer
	ftime(&endTotalSort);
	printBuffer(inputBuffer);

	// Closing the connection establish on the beginning of the program
	closeClient( &client );
	ftime(&end);
	
	
	return 0;
}

// implementation of the printfBuffer function
void printBuffer(int *vector) {
	int i;
	for ( i = 0; i < 1000; i++ ) {
		if( i%10 == 0 ) printf("\n");
		printf(" index %4d - %5d", i, vector[i]);
	}
	printf("\n\n");
}

// function to calculate the time interval of execution
float time_dif(struct timeb end, struct timeb start) {
	float dif;
	return dif = (float)((end.time - start.time) + 0.001*(end.millitm - start.millitm));
}

















